package com.chen.pspms.controller;

import com.chen.pspms.entity.CompMajorStatistics;
import com.chen.pspms.entity.CompanyInfo;
import com.chen.pspms.entity.GoveMajorStatistics;
import com.chen.pspms.service.CompMajorStatisticsService;
import com.chen.pspms.service.CompanyService;
import com.chen.pspms.service.GovernmentStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
@RequestMapping("/governmentStaff")
public class GovernmentStaffController {
    @Autowired
    GovernmentStaffService governmentStaffService;
    @Autowired
    CompanyService companyService;
    @Autowired
    CompMajorStatisticsService compMajorStatisticsService;

    @RequestMapping("/list")
    public String list(Model model){
        //返回： 市：查询数，危险源数
        //获取company列表  获取city列表
        List<CompanyInfo> companyList=companyService.findAll();
        List<CompMajorStatistics> compMajorStatisticsList = compMajorStatisticsService.getListByCompany(companyList);
        //弄一个城市set
        Map<String, GoveMajorStatistics> cityMap=new HashMap<>();
        GoveMajorStatistics goveMajorStatistics;
        for(CompMajorStatistics compMajorStatistics:compMajorStatisticsList){
            String city=compMajorStatistics.getCompany().getCity();
            if(!cityMap.containsKey(city)){
                goveMajorStatistics= new GoveMajorStatistics(city,0,0);
            }else{
                goveMajorStatistics= cityMap.get(city);
            }
            goveMajorStatistics.setInspectionNum(goveMajorStatistics.getInspectionNum()+compMajorStatistics.getInspectionNum());
            goveMajorStatistics.setMajorhazardNum(goveMajorStatistics.getMajorhazardNum()+goveMajorStatistics.getMajorhazardNum());
            cityMap.put(city,goveMajorStatistics);
        }
        List<GoveMajorStatistics> goveMajorStatisticsList=new ArrayList<>();
        for(GoveMajorStatistics goveMajorStatistics1:cityMap.values()){
            goveMajorStatisticsList.add(goveMajorStatistics1);
        }
        model.addAttribute("goveMajorStatisticsList",goveMajorStatisticsList);
        return "预警防控/政府部门监管和领导责任落实预警";
    }
}
